//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


namespace Shp.Runtime.Sitka.ServiceAgent.EntityMappers
{
    using System;
    using Shp.Runtime.Sitka.ServiceGateway.SitkaClient;
    using Shp.Runtime.Sitka.ServiceAgent.Managers;
    using Shp.Runtime.Sitka.ServiceAgent.Entities;
    using System.Collections.Generic;

    public class GenericExtensibleEntityMapper : EntityMapper<GenericExtensibleEntity>
    {
        public override GenericExtensibleEntity ToType(Entity flexibleEntity)
        {
            GenericExtensibleEntity entity = new GenericExtensibleEntity(flexibleEntity.Kind, new Guid(flexibleEntity.Id));

            foreach (string key in flexibleEntity.Properties.Keys)
            {
                entity.Fields.Add(new Field(key, flexibleEntity.Properties[key]));
            }

            return entity;
        }

        public override Entity FromType(GenericExtensibleEntity entity)
        {
            Entity sitkaEntity = new Entity();
            sitkaEntity.Id = entity.Id.ToString();
            sitkaEntity.Kind = entity.Kind;
            sitkaEntity.Properties = new Dictionary<string, object>();

            foreach (Field field in entity.Fields)
            {
                sitkaEntity.Properties.Add(field.Name, field.Value);
            }

            return sitkaEntity;
        }
    }
}
